<?php

class Pr_user extends Core {
    
    function __construct() {
        
        parent::__construct_admin();
        
        if (!$this->usr->check_rights('admin')) {
            
            show_error('Нет доступа!!!');
        }
        
        $this->load->language('user');
        $this->load->model('moduser');
    }
    
    public function index() {
        
        $this->load->library('pagination');
        
        foreach($this->moduser->get_groups()->result_array() as $row) {
            
            $vars['groups'][$row['id']] = $row['title'];
        }
        
        $vars['pagination'] = $this->pagination->create('admin/modul/user/index', $this->moduser->count_users(), $this->config->get('pagination', 'core'));
        $vars['users'] = $this->moduser->get_users(array(), $this->config->get('pagination', 'core'), $this->pagination->offset())->result_array();
                
        parent::render($vars, 'admin/index', true);
    }
    
    public function add() {
        
        $this->load->helper('form');
        
        if ( $this->input->post('add', TRUE) ) {
            
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('user_login', 'lang:user_login', 'trim|required|min_length[2]|alpha_dash|xss_clean');
            $this->form_validation->set_rules('user_mail', 'lang:user_mail', 'trim|required|valid_email|xss_clean');
            $this->form_validation->set_rules('user_password', 'lang:user_password', 'trim|required|xss_clean');
            $this->form_validation->set_rules('user_group', 'lang:user_group', 'trim|required|xss_clean');
            
            if ( $this->form_validation->run() ) {
                
                if ($this->moduser->add(set_value('user_login'), set_value('user_mail'), set_value('user_password'), set_value('user_group'))) {
                    
                    $vars['notification'] = lang('user_success_register');
                }
                else {
                    
                    $vars['notification'] = $this->moduser->get_errors();
                }
            }
            else {
                
                $vars['notification'] = $this->form_validation->error_string();
            }
        }
        
        foreach($this->moduser->get_groups()->result_array() as $row) {
            
            $vars['groups'][$row['id']] = $row['title'];
        }
        
        parent::render($vars, 'admin/add_user', true);
    }
    
    public function edit($id) {
        
        $this->load->helper('form');
        
        if ( $this->input->post('edit', TRUE) ) {
            
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('user_login', 'lang:user_login', 'trim|required|min_length[2]|alpha_dash|xss_clean');
            $this->form_validation->set_rules('user_mail', 'lang:user_mail', 'trim|required|valid_email|xss_clean');
            $this->form_validation->set_rules('user_password', 'lang:user_password', 'trim|xss_clean');
            $this->form_validation->set_rules('user_group', 'lang:user_group', 'trim|required|xss_clean');
            
            if ( $this->form_validation->run() ) {
                
                if ($this->moduser->edit($id, set_value('user_login'), set_value('user_mail'), set_value('user_password'), set_value('user_group'))) {
                    
                    $vars['notification'] = lang('user_success_edit');
                }
                else {
                    
                    $vars['notification'] = $this->moduser->get_errors();
                }
            }
            else {
                
                $vars['notification'] = $this->form_validation->error_string();
            }
        }
        
        foreach($this->moduser->get_groups()->result_array() as $row) {
            
            $vars['groups'][$row['id']] = $row['title'];
        }
        
        $vars['user'] = $this->usr->get($id, 'user_id')->row_array();
        
        parent::render($vars, 'admin/edit_user', true);
    }
    
    public function delete($id) {
        
        $this->moduser->delete_user($id);
        modul_redirect('modul/user');
    }
    
    public function groups() {
        
        $this->set_bread_crumbs('Группы', 'modul/user/groups');
        $vars['groups'] = $this->moduser->get_groups()->result_array();
        
        parent::render($vars, 'admin/groups', true);
    }
    
    public function add_group() {
        
        $this->load->helper('form');
        
        if ( $this->input->post('add', TRUE) ) {
            
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('title', 'lang:group_title', 'trim|required|xss_clean');
            $this->form_validation->set_rules('group_copy', 'lang:group_title', 'trim|required|xss_clean');
            
            if ( $this->form_validation->run() ) {
                
                $this->moduser->add_group(set_value('title'), set_value('group_copy'));
                $vars['notification'] = lang('group_success_add');
            }
            else {
                
                $vars['notification'] = $this->form_validation->error_string();
            }
        }
        
        foreach($this->moduser->get_groups()->result_array() as $row) {
            
            $vars['groups'][$row['id']] = $row['title'];
        }
        
        parent::render($vars, 'admin/groups_add', true);
    }
    
    public function edit_group($id) {
        
        $this->load->helper('form');
        
        if ( $this->input->post('edit', TRUE) ) {
            
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('title', 'lang:group_title', 'trim|required|xss_clean');
            $this->form_validation->set_rules('is_guest', 'lang:group_is_guest', 'trim|xss_clean');
            $this->form_validation->set_rules('is_register', 'lang:group_is_register', 'trim|xss_clean');
            
            if ( $this->form_validation->run() ) {
                
                $this->moduser->edit_group($id, set_value('title'), set_value('is_guest', 0), set_value('is_register', 0));
                $vars['notification'] = lang('group_success_edit');
            }
            else {
                
                $vars['notification'] = $this->form_validation->error_string();
            }
        }
        
        $vars['group'] = $this->moduser->get_groups($id)->row_array();
        
        parent::render($vars, 'admin/groups_edit', true);
    }
    
    public function delete_group($id) {
        
        $this->load->helper('form');
        
        if ( $this->input->post('add', TRUE) ) {
            
            $this->load->library('form_validation');
            
            $this->form_validation->set_rules('group_move', 'lang:group_move', 'trim|required|xss_clean');
            
            if ( $this->form_validation->run() ) {
                
                $this->moduser->delete_group($id, set_value('group_move'));
                $vars['notification'] = lang('group_move');
            }
            else {
                
                $vars['notification'] = $this->form_validation->error_string();
            }
        }
        
        $vars['group'] = $this->moduser->get_groups($id)->row_array();
        
        foreach($this->moduser->get_groups()->result_array() as $row) {
            
            if ($id != $row['id']) {
            
                $vars['groups'][$row['id']] = $row['title'];
            }
        }
        
        parent::render($vars, 'admin/groups_delete', true);
    }
}